<template>
  <div>
    <el-dialog
      title="添加角色"
      :close-on-click-modal ="false"
      @close="addDialogClosed"
      :visible.sync="addDialogVisible"
      width="50%">
      <!-- 内容主体区域 -->
      <el-form :model="addRolesFrom" :rules="addRolesRules" ref="addRolesRef" >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRolesFrom.roleName" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input type="textarea" :rows="2"  v-model="addRolesFrom.roleDesc" placeholder="请输入角色描述"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRelos">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'VueShopAddRoles',

  data() {
    return {
      addDialogVisible:false,
      addRolesFrom:{
        roleName:'',
        roleDesc:''
      },
      addRolesRules:{
        roleName:[
          {required: true, message:'输入角色名称', triggt:'blur'}
        ],
        roleDesc:[
          {required: true, message:'输入角色描述', triggt:'blur'}
        ]
    }
    };
  },
  methods: {
    /**
     * @name addRelos()
     * @提交角色添加
     */
     addRelos(){
      this.$refs.addRolesRef.validate(async valied => {
        if(!valied) return ;
        const {data:res} = await this.$http.post('roles',{
          ...this.addRolesFrom
        })
        if(res.meta.status !== 201){
          return this.$message.error('角色创建失败');
        }
        this.$message.success('创建成功');
        this.addDialogVisible = false;
        this.$emit('getRolesList');
      })
    },
    /**
     * @name addDialogClosed()
     * @监听关闭添加角色对话框
     */
    addDialogClosed(){
      this.$refs.addRolesRef.resetFields();
    }
  },
};
</script>

<style lang="scss" scoped>

</style>